library(MatchIt) 
library(Matching)
library(ggplot2)
library(stargazer)
library(xtable)

# IMPORTANT NOTICE: YOU NEED TO SET THE WORKING DIRECTORY TO THE "Replication_File" FOLDER

setwd("~/Replication_File")

####### Note: All results are based on packages MatchIT 4.3.3 and Matching 4.10.8.
####### Different version may cause different results or errors



mydata <- readRDS("Data/Balance_Data.rds")

####### balance table for matched listed firms and whole data
mydata$price = as.numeric(mydata$price)
mydata$merge = as.logical(mydata$merge)
mydata$area = as.numeric(mydata$area)
mydata$usage_year= as.numeric(mydata$usage_year)
mydata$land_level=as.factor(mydata$land_level)
mydata$land_level=as.numeric(mydata$land_level)

mb  <- MatchBalance(merge~area + price + usage_year + land_level, 
                    data=mydata, match.out=NULL)

summary(mb)

table<- setNames(data.frame(matrix(ncol = 4, nrow = 4)), c("Mean Treatment", "Mean Control", "Mean Difference","P Value"))

table[1,1]<-mb[["BeforeMatching"]][[1]][["mean.Tr"]]

table[1,2]<-mb[["BeforeMatching"]][[1]][["mean.Co"]]

table[1,3]<-mb[["BeforeMatching"]][[1]][["mean.Tr"]]-mb[["BeforeMatching"]][[1]][["mean.Co"]]

table[1,4]<-mb[["BeforeMatching"]][[1]][["p.value"]]




table[2,1]<-mb[["BeforeMatching"]][[2]][["mean.Tr"]]

table[2,2]<-mb[["BeforeMatching"]][[2]][["mean.Co"]]

table[2,3]<-mb[["BeforeMatching"]][[2]][["mean.Tr"]]-mb[["BeforeMatching"]][[2]][["mean.Co"]]

table[2,4]<-mb[["BeforeMatching"]][[2]][["p.value"]]



table[3,1]<-mb[["BeforeMatching"]][[3]][["mean.Tr"]]

table[3,2]<-mb[["BeforeMatching"]][[3]][["mean.Co"]]

table[3,3]<-mb[["BeforeMatching"]][[3]][["mean.Tr"]]-mb[["BeforeMatching"]][[3]][["mean.Co"]]

table[3,4]<-mb[["BeforeMatching"]][[3]][["p.value"]]




table[4,1]<-mb[["BeforeMatching"]][[4]][["mean.Tr"]]

table[4,2]<-mb[["BeforeMatching"]][[4]][["mean.Co"]]

table[4,3]<-mb[["BeforeMatching"]][[4]][["mean.Tr"]]-mb[["BeforeMatching"]][[4]][["mean.Co"]]

table[4,4]<-mb[["BeforeMatching"]][[4]][["p.value"]]

table<- round(table,2)

rownames(table) <- c("Area", "Price", "Years of Usage", "Land Level")

xtable(table)



###### matched auction sample with all firms have land transition

mydata <- readRDS("Balance_Data_Auction.rds")


mydata$price = as.numeric(mydata$price)
mydata$merge = as.logical(mydata$merge)
mydata$area = as.numeric(mydata$area)
mydata$usage_year= as.numeric(mydata$usage_year)
mydata$land_level=as.factor(mydata$land_level)
mydata$land_level=as.numeric(mydata$land_level)

mydata$logtotal_asset=as.numeric(mydata$logtotal_asset)
mydata$logtotal_employee=as.numeric(mydata$logtotal_employee)
mydata$logtotal_debt=as.numeric(mydata$logtotal_debt)
mydata$profit_per_share=as.numeric(mydata$profit_per_share)
mydata$GPC=as.numeric(mydata$GPC)
mydata$LPC=as.numeric(mydata$LPC)
mydata$LE=as.numeric(mydata$LE)
mydata$pc_percent=as.numeric(mydata$pc_percent)



mb  <- MatchBalance(merge~area + price + usage_year + land_level +logtotal_asset
                    +logtotal_employee+logtotal_debt+profit_per_share+GPC+LPC
                    +LE+pc_percent, 
                    data=mydata, match.out=NULL)

summary(mb)

table<- setNames(data.frame(matrix(ncol = 4, nrow = 12)), c("Mean Treatment", "Mean Control", "Mean Difference","P Value"))

table[1,1]<-mb[["BeforeMatching"]][[1]][["mean.Tr"]]

table[1,2]<-mb[["BeforeMatching"]][[1]][["mean.Co"]]

table[1,3]<-mb[["BeforeMatching"]][[1]][["mean.Tr"]]-mb[["BeforeMatching"]][[1]][["mean.Co"]]

table[1,4]<-mb[["BeforeMatching"]][[1]][["p.value"]]




table[2,1]<-mb[["BeforeMatching"]][[2]][["mean.Tr"]]

table[2,2]<-mb[["BeforeMatching"]][[2]][["mean.Co"]]

table[2,3]<-mb[["BeforeMatching"]][[2]][["mean.Tr"]]-mb[["BeforeMatching"]][[2]][["mean.Co"]]

table[2,4]<-mb[["BeforeMatching"]][[2]][["p.value"]]



table[3,1]<-mb[["BeforeMatching"]][[3]][["mean.Tr"]]

table[3,2]<-mb[["BeforeMatching"]][[3]][["mean.Co"]]

table[3,3]<-mb[["BeforeMatching"]][[3]][["mean.Tr"]]-mb[["BeforeMatching"]][[3]][["mean.Co"]]

table[3,4]<-mb[["BeforeMatching"]][[3]][["p.value"]]




table[4,1]<-mb[["BeforeMatching"]][[4]][["mean.Tr"]]

table[4,2]<-mb[["BeforeMatching"]][[4]][["mean.Co"]]

table[4,3]<-mb[["BeforeMatching"]][[4]][["mean.Tr"]]-mb[["BeforeMatching"]][[4]][["mean.Co"]]

table[4,4]<-mb[["BeforeMatching"]][[4]][["p.value"]]



table[5,1]<-mb[["BeforeMatching"]][[5]][["mean.Tr"]]

table[5,2]<-mb[["BeforeMatching"]][[5]][["mean.Co"]]

table[5,3]<-mb[["BeforeMatching"]][[5]][["mean.Tr"]]-mb[["BeforeMatching"]][[5]][["mean.Co"]]

table[5,4]<-mb[["BeforeMatching"]][[5]][["p.value"]]



table[6,1]<-mb[["BeforeMatching"]][[6]][["mean.Tr"]]

table[6,2]<-mb[["BeforeMatching"]][[6]][["mean.Co"]]

table[6,3]<-mb[["BeforeMatching"]][[6]][["mean.Tr"]]-mb[["BeforeMatching"]][[6]][["mean.Co"]]

table[6,4]<-mb[["BeforeMatching"]][[6]][["p.value"]]



table[7,1]<-mb[["BeforeMatching"]][[7]][["mean.Tr"]]

table[7,2]<-mb[["BeforeMatching"]][[7]][["mean.Co"]]

table[7,3]<-mb[["BeforeMatching"]][[7]][["mean.Tr"]]-mb[["BeforeMatching"]][[7]][["mean.Co"]]

table[7,4]<-mb[["BeforeMatching"]][[7]][["p.value"]]



table[8,1]<-mb[["BeforeMatching"]][[8]][["mean.Tr"]]

table[8,2]<-mb[["BeforeMatching"]][[8]][["mean.Co"]]

table[8,3]<-mb[["BeforeMatching"]][[8]][["mean.Tr"]]-mb[["BeforeMatching"]][[8]][["mean.Co"]]

table[8,4]<-mb[["BeforeMatching"]][[8]][["p.value"]]



table[9,1]<-mb[["BeforeMatching"]][[9]][["mean.Tr"]]

table[9,2]<-mb[["BeforeMatching"]][[9]][["mean.Co"]]

table[9,3]<-mb[["BeforeMatching"]][[9]][["mean.Tr"]]-mb[["BeforeMatching"]][[9]][["mean.Co"]]

table[9,4]<-mb[["BeforeMatching"]][[9]][["p.value"]]



table[10,1]<-mb[["BeforeMatching"]][[10]][["mean.Tr"]]

table[10,2]<-mb[["BeforeMatching"]][[10]][["mean.Co"]]

table[10,3]<-mb[["BeforeMatching"]][[10]][["mean.Tr"]]-mb[["BeforeMatching"]][[10]][["mean.Co"]]

table[10,4]<-mb[["BeforeMatching"]][[10]][["p.value"]]



table[11,1]<-mb[["BeforeMatching"]][[11]][["mean.Tr"]]

table[11,2]<-mb[["BeforeMatching"]][[11]][["mean.Co"]]

table[11,3]<-mb[["BeforeMatching"]][[11]][["mean.Tr"]]-mb[["BeforeMatching"]][[11]][["mean.Co"]]

table[11,4]<-mb[["BeforeMatching"]][[11]][["p.value"]]


table[12,1]<-mb[["BeforeMatching"]][[12]][["mean.Tr"]]

table[12,2]<-mb[["BeforeMatching"]][[12]][["mean.Co"]]

table[12,3]<-mb[["BeforeMatching"]][[12]][["mean.Tr"]]-mb[["BeforeMatching"]][[12]][["mean.Co"]]

table[12,4]<-mb[["BeforeMatching"]][[12]][["p.value"]]

table<- round(table,4)

xtable(table, digits = 4)
